package wiki.zimo.certificate_manage.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import wiki.zimo.certificate_manage.constant.Constant;
import wiki.zimo.certificate_manage.dao.StudentDao;
import wiki.zimo.certificate_manage.dao.StudentPrizeInfoDao;
import wiki.zimo.certificate_manage.dao.TeacherDao;
import wiki.zimo.certificate_manage.domain.Student;
import wiki.zimo.certificate_manage.domain.Teacher;
import wiki.zimo.certificate_manage.domain.User;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 *
 * @author 子墨
 * @date 2018/10/13 15:40
 * @describe 处理首页的请求
 */
@Controller
@RequestMapping("")
public class IndexController {

    @Autowired
    private StudentDao studentDao;
    @Autowired
    private TeacherDao teacherDao;
    @Autowired
    private StudentPrizeInfoDao prizeInfoDao;

    /**
     * 处理访问首页的请求
     * @author 子墨
     * @date 2018/10/13 15:41
     * @params [map]
     * @return java.lang.String
     */
    @RequestMapping("/index")
    public String index(Map<String,Object> map,HttpSession session){

        User user = (User) session.getAttribute(Constant.USER);
        if (user == null){
            return "index";
        }
        String role = user.getRole();
        switch (role){
            case Constant.ROLE_LEADER:
                leaderDataPut(map);
                break;
            case Constant.ROLE_TEACHER:
                teacherDataPut(map,user.getTeacher());
                break;
            case Constant.ROLE_STUDENT:
                studentDataPut(map,user.getStudent());
                break;
        }

        long countOfStudent = studentDao.count();
        long countOfTeacher = teacherDao.count();

        map.put("countOfStudent",countOfStudent);
        map.put("countOfTeacher",countOfTeacher);

        return "index";
    }

    /**
     * @auther: 子墨
     * @date: 2018/10/28 20:22
     * @describe: 学生登录时的统计信息
     * @param: [map, student]
     * @return: void
     * @version v1.0
     */
    private void studentDataPut(Map<String,Object> map, Student student) {
        long countOfPrize = prizeInfoDao.countAllByStuId(student.getId());

        long countOfSpecialPrize = prizeInfoDao.countAllByPrizeGradeAndStuId(Constant.SPECIAL_PRIZE_GRADE,student.getId());
        long countOfFirstPrize = prizeInfoDao.countAllByPrizeGradeAndStuId(Constant.FIRST_PRIZE_GRADE,student.getId());
        long countOfSecondPrize = prizeInfoDao.countAllByPrizeGradeAndStuId(Constant.SECOND_PRIZE_GRADE,student.getId());
        long countOfThirdPrize = prizeInfoDao.countAllByPrizeGradeAndStuId(Constant.THIRD_PRIZE_GRADE,student.getId());

        long countOfContryLevel = prizeInfoDao.countAllByPrizeLevelAndStuId(Constant.COUNTRY_PRIZE_LEVEL,student.getId());
        long countOfProvinceLevel = prizeInfoDao.countAllByPrizeLevelAndStuId(Constant.PROVINCE_PRIZE_LEVEL,student.getId());
        long countOfCityLevel = prizeInfoDao.countAllByPrizeLevelAndStuId(Constant.CITY_PRIZE_LEVEL,student.getId());
        long countOfSchoolLevel = prizeInfoDao.countAllByPrizeLevelAndStuId(Constant.SCHOOL_PRIZE_LEVEL,student.getId());

        map.put("countOfPrize",countOfPrize);

        map.put("countOfSpecialPrize",countOfSpecialPrize);
        map.put("countOfFirstPrize",countOfFirstPrize);
        map.put("countOfSecondPrize",countOfSecondPrize);
        map.put("countOfThirdPrize",countOfThirdPrize);

        map.put("countOfContryLevel",countOfContryLevel);
        map.put("countOfProvinceLevel",countOfProvinceLevel);
        map.put("countOfCityLevel",countOfCityLevel);
        map.put("countOfSchoolLevel",countOfSchoolLevel);
    }

    /**
     * @auther: 子墨
     * @date: 2018/10/28 20:14
     * @describe: 辅导员登录时的统计数据
     * @param: [map]
     * @return: void
     * @version v1.0
     */
    private void teacherDataPut(Map<String,Object> map, Teacher teacher) {
        long countOfPrize = prizeInfoDao.countAllByTeacherId(teacher.getId());

        long countOfSpecialPrize = prizeInfoDao.countAllByPrizeGradeAndTeacherId(Constant.SPECIAL_PRIZE_GRADE,teacher.getId());
        long countOfFirstPrize = prizeInfoDao.countAllByPrizeGradeAndTeacherId(Constant.FIRST_PRIZE_GRADE,teacher.getId());
        long countOfSecondPrize = prizeInfoDao.countAllByPrizeGradeAndTeacherId(Constant.SECOND_PRIZE_GRADE,teacher.getId());
        long countOfThirdPrize = prizeInfoDao.countAllByPrizeGradeAndTeacherId(Constant.THIRD_PRIZE_GRADE,teacher.getId());

        long countOfContryLevel = prizeInfoDao.countAllByPrizeLevelAndTeacherId(Constant.COUNTRY_PRIZE_LEVEL,teacher.getId());
        long countOfProvinceLevel = prizeInfoDao.countAllByPrizeLevelAndTeacherId(Constant.PROVINCE_PRIZE_LEVEL,teacher.getId());
        long countOfCityLevel = prizeInfoDao.countAllByPrizeLevelAndTeacherId(Constant.CITY_PRIZE_LEVEL,teacher.getId());
        long countOfSchoolLevel = prizeInfoDao.countAllByPrizeLevelAndTeacherId(Constant.SCHOOL_PRIZE_LEVEL,teacher.getId());

        map.put("countOfPrize",countOfPrize);

        map.put("countOfSpecialPrize",countOfSpecialPrize);
        map.put("countOfFirstPrize",countOfFirstPrize);
        map.put("countOfSecondPrize",countOfSecondPrize);
        map.put("countOfThirdPrize",countOfThirdPrize);

        map.put("countOfContryLevel",countOfContryLevel);
        map.put("countOfProvinceLevel",countOfProvinceLevel);
        map.put("countOfCityLevel",countOfCityLevel);
        map.put("countOfSchoolLevel",countOfSchoolLevel);
    }


    /**
     * @auther: 子墨
     * @date: 2018/10/28 20:13
     * @describe: 分管领导登录时的统计数据
     * @param: [map]
     * @return: void
     * @version v1.0
     */
    private void leaderDataPut(Map<String,Object> map) {
        long countOfPrize = prizeInfoDao.count();

        long countOfSpecialPrize = prizeInfoDao.countAllByPrizeGrade(Constant.SPECIAL_PRIZE_GRADE);
        long countOfFirstPrize = prizeInfoDao.countAllByPrizeGrade(Constant.FIRST_PRIZE_GRADE);
        long countOfSecondPrize = prizeInfoDao.countAllByPrizeGrade(Constant.SECOND_PRIZE_GRADE);
        long countOfThirdPrize = prizeInfoDao.countAllByPrizeGrade(Constant.THIRD_PRIZE_GRADE);

        long countOfContryLevel = prizeInfoDao.countAllByPrizeLevel(Constant.COUNTRY_PRIZE_LEVEL);
        long countOfProvinceLevel = prizeInfoDao.countAllByPrizeLevel(Constant.PROVINCE_PRIZE_LEVEL);
        long countOfCityLevel = prizeInfoDao.countAllByPrizeLevel(Constant.CITY_PRIZE_LEVEL);
        long countOfSchoolLevel = prizeInfoDao.countAllByPrizeLevel(Constant.SCHOOL_PRIZE_LEVEL);

        map.put("countOfPrize",countOfPrize);

        map.put("countOfSpecialPrize",countOfSpecialPrize);
        map.put("countOfFirstPrize",countOfFirstPrize);
        map.put("countOfSecondPrize",countOfSecondPrize);
        map.put("countOfThirdPrize",countOfThirdPrize);

        map.put("countOfContryLevel",countOfContryLevel);
        map.put("countOfProvinceLevel",countOfProvinceLevel);
        map.put("countOfCityLevel",countOfCityLevel);
        map.put("countOfSchoolLevel",countOfSchoolLevel);
    }
}
